package it.uniroma1.nlp.cablegate;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

	public class Sentiment {
		// you need to change it
		private String pathToSWN = "/home/xuwen/Desktop/CS224N/projects/final/SentiWordNet.txt";
		private static String pythonDump = "/home/xuwen/Desktop/CS224N/projects/final/cablegate/cable/i2f";
		private static String output = "/home/xuwen/Desktop/CS224N/projects/final/cablegate/cable/sentscore.txt";
		private HashMap<String, Double> _dict;

		public Sentiment(){
			_dict = new HashMap<String, Double>();
			HashMap<String, Vector<Double>> _temp = new HashMap<String, Vector<Double>>();
			try{
				BufferedReader csv =  new BufferedReader(new FileReader(pathToSWN));
				String line = "";			
				while((line = csv.readLine()) != null)
				{
					String[] data = line.split("\t");
					
					//Positive - Negative
					Double score = Double.parseDouble(data[2])-Double.parseDouble(data[3]);
					String[] words = data[4].split(" ");
					for(String w:words)
					{
						String[] w_n = w.split("#");
						w_n[0] += "#"+data[0];
						int index = Integer.parseInt(w_n[1])-1;
						if(_temp.containsKey(w_n[0]))
						{
							Vector<Double> v = _temp.get(w_n[0]);
							if(index>v.size())
								for(int i = v.size();i<index; i++)
									v.add(0.0);
							v.add(index, score);
							_temp.put(w_n[0], v);
						}
						else
						{
							Vector<Double> v = new Vector<Double>();
							for(int i = 0;i<index; i++)
								v.add(0.0);
							v.add(index, score);
							_temp.put(w_n[0], v);
						}
					}
				}
				Set<String> temp = _temp.keySet();
				for (Iterator<String> iterator = temp.iterator(); iterator.hasNext();) {
					String word = (String) iterator.next();
					Vector<Double> v = _temp.get(word);
					double score = 0.0;
					double sum = 0.0;
					for(int i = 0; i < v.size(); i++)
						score += ((double)1/(double)(i+1))*v.get(i);
					for(int i = 1; i<=v.size(); i++)
						sum += (double)1/(double)i;
					score /= sum;
					String sent = "";				
					if(score > 0.75)
						sent = "strong_positive";
					else
					if(score > 0.25 && score <= 0.75)
						sent = "positive";
					else
					if(score > 0 && score <= 0.25)
						sent = "weak_positive";
					else
					if(score == 0)
						sent = "neutral";
					if(score < 0 && score >= -0.25)
						sent = "weak_negative";
					else
					if(score < -0.25 && score >= -0.75)
						sent = "negative";
					else
					if(score < -0.75)
						sent = "strong_negative";
					_dict.put(word, score);
				}
			}
			catch(Exception e){e.printStackTrace();}
		}

		public Double extract(String word, String pos)
		{
			Double score = _dict.get(word+"#"+pos);
			return score == null ? 0.0 : score;
		}
		
		public static List<String> readPythonDump() {
			List<String> pythonout = new ArrayList<String>();
			try {
				BufferedReader rdr =  new BufferedReader(new FileReader(pythonDump));
				String line = "";			
				while((line = rdr.readLine()) != null) {
					String[] data = line.split("'");
					if (data.length == 2) {
						String[] origword = data[1].split("#");
						if (origword.length != 2) pythonout.add("");
						else pythonout.add(origword[1]);
					}
				}

			} catch(Exception e){e.printStackTrace();}
			return pythonout;
		}
		
		public static void main(String[] args) {
			Sentiment s = new Sentiment();
			List<String> mystrings = readPythonDump();
			List<Double> myscores = new ArrayList<Double>();
			for (String astring : mystrings) {
				myscores.add(s.extract(astring, "a"));
			}
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter(output));
				for (Double ascore : myscores) {
					out.write(ascore.toString() + '\n');
				}
				out.close();
			} catch (Exception e) {e.printStackTrace();}
			System.out.println(mystrings.toString());
			System.out.println(mystrings.size());
			System.out.println(myscores.get(mystrings.indexOf("crazy")));
			System.out.println(s.extract("crazy", "a"));
		}
	}